package org.btik.light.server.dev.settings;

import org.btik.light.server.platform.common.api.bean.common.StatusResult;
import org.btik.light.server.platform.common.api.bean.settings.SystemSettings;
import org.btik.light.server.platform.common.api.dao.LightDao;
import org.btik.light.server.platform.common.api.service.SystemSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import static org.btik.light.server.platform.common.api.bean.common.ResultFactory.success;

/**
 * @author lustre
 * @since 2023/6/29 14:22
 */
@Service
public class SystemSettingsServiceImpl implements SystemSettingsService {
    @Autowired
    private LightDao lightDao;


    @Override
    public StatusResult<Map<String, String>> getSysSettings() {
        SystemSettings systemSettings = lightDao.get(SYSTEM_SETTINGS, SystemSettings.class);
        if (systemSettings == null) {
            return success();
        }
        return success(systemSettings.getValues());
    }

    @Override
    public StatusResult<Void> updateSettings(Map<String, String> settings) {
        SystemSettings systemSettings = lightDao.get(SYSTEM_SETTINGS, SystemSettings.class);
        if (systemSettings == null) {
            systemSettings = new SystemSettings();
        }
        systemSettings.getValues().putAll(settings);
        lightDao.save(systemSettings);
        return success();
    }
}
